/*==manifest.json==
    	{
    		"name": "饭团影视",
    		"version": 1.0,
    		"home":"https://www.fantuan.run",
    		"matches": "/player",
    		"update": "https://gitee.com/joinli/dodge/raw/master/extension/extension_fantuan.js",
    		"author":"JoinLi",
    		"ua":"Mobile"
    	}
    */

//页面开始加载
function onPageStarted() {

}

//页面加载完成
function onPageFinished() {
	getPlayData();
}

function getPlayData() {
	const panels = document.querySelectorAll('.module-play-list');
	const title = document.title.split(' ')[0].replace("在线播放", "");
	const result = {
		title: title,
		list: []
	};
	panels.forEach((panel, index) => {
		const panelData = {
			title: '视频源' + (index + 1).toString(), // 标题为1, 2, 3...
			play_list: []
		};

		// 获取当前panel中的所有链接
		const links = panel.querySelectorAll('.module-play-list-link');
		links.forEach(link => {
			const name = link.querySelector('span').textContent;
			const href = link.getAttribute('href');
			const url = window.location.origin + href;
			const address = window.location.href;
			const isSelect = address.includes(href);
			panelData.play_list.push({
				name,
				url,
				isSelect
			});

		});
		// 将当前panel的数据添加到结果中
		result.list.push(panelData);
	});
	const data = JSON.stringify({
		event: 'playData',
		data: result
	});
	if (window.dartJavascriptChannel) {
		window.dartJavascriptChannel.postMessage(data);
	}
	window.dartJavascriptChannel.postMessage(data);
	const element = document.elementFromPoint(200, 200);
	if (element.tagName.toLowerCase() === 'iframe') {
		const iframeDoc = element.contentDocument || element.contentWindow.document;
		iframeDoc.elementFromPoint(200, 200).click();
	}

}
